<?php
/**
 * @package scuttle
 * @file /forms/BookmarksEditForm.class.php
 **/

if (!defined('XOOPS_ROOT_PATH')) exit();

require_once XOOPS_ROOT_PATH.'/core/XCube_ActionForm.class.php';
require_once XOOPS_MODULE_PATH.'/legacy/class/Legacy_Validator.class.php';

class Scuttle_BookmarksEditForm extends XCube_ActionForm
{

	var $mOldFileName = null;
	var $_mIsNew = null;
	var $mFormFile = null;

	function getTokenName()
	{
		return 'module.scuttle.BookmarksEdit.Form.TOKEN.' . $this->get('bookmarks');
	}

	function prepare()
	{
		$this->mFormProperties['bId'] =& new XCube_IntProperty('bId');
		$this->mFormProperties['uId'] =& new XCube_IntProperty('uId');
		$this->mFormProperties['bIp'] =& new XCube_StringProperty('bIp');
		$this->mFormProperties['bStatus'] =& new XCube_StringProperty('bStatus');
		$this->mFormProperties['bDatetime'] =& new XCube_StringProperty('bDatetime');
		$this->mFormProperties['bModified'] =& new XCube_StringProperty('bModified');
		$this->mFormProperties['bTitle'] =& new XCube_StringProperty('bTitle');
		$this->mFormProperties['bAddress'] =& new XCube_TextProperty('bAddress');
		$this->mFormProperties['bDescription'] =& new XCube_StringProperty('bDescription');
		$this->mFormProperties['image'] =& new XCube_ImageFileProperty('image');

		$this->mFieldProperties['bId'] =& new XCube_FieldProperty($this);
		$this->mFieldProperties['bId']->setDependsByArray(array('required'));
		$this->mFieldProperties['bId']->addMessage('required', _MD_SC_ERROR_REQUIRED, _MD_SC_BOOKMARKS_BID);

		$this->mFieldProperties['image'] =& new XCube_FieldProperty($this);
		$this->mFieldProperties['image']->setDependsByArray(array('extension'));
		$this->mFieldProperties['image']->addMessage('extension', _MD_SC_ERROR_EXTENSION);
		$this->mFieldProperties['image']->addVar('extension', 'jpg,gif,png');
	}


	function validateTerm()
	{
		if ($this->get('bAddress') != null) {
			$bAddress = trim($this->get('bAddress')) ;
			$mHandler =& xoops_getmodulehandler('bookmarks');
			$mCriteria =& new CriteriaCompo();
			$mCriteria->add(new Criteria('bAddress', $bAddress));
			if ($this->get('bId') >0) {
				$mCriteria->add(new Criteria('bId', $this->get('bId'),'<>'));
			}
			$tableName =& $mHandler->getObjects($mCriteria, true);
			if ($tableName != null) {
				$this->addErrorMessage(_MD_SC_ERROR_REPEATED);
			}
		}
	}

	function load(&$obj)
	{
		$this->set('bId'          , $obj->get('bId'));
		$this->set('uId'          , $obj->get('uId'));
		$this->set('bIp'          , $obj->get('bIp'));
		$this->set('bStatus'      , $obj->get('bStatus'));
		$this->set('bDatetime'    , $obj->get('bDatetime'));
		$this->set('bModified'    , $obj->get('bModified'));
		$this->set('bTitle'       , $obj->get('bTitle'));
		$this->set('bAddress'     , $obj->get('bAddress'));
		$this->set('bDescription' , $obj->get('bDescription'));

		$this->_mIsNew = $obj->isNew();
		$this->mOldFileName = $obj->get('image');

	}

	function update(&$obj)
	{
		global $xoopsUser;

		$obj->set('bId'          , $this->get('bId'));
		$obj->set('uId'          , $xoopsUser->uid());
		$obj->set('bIp'          , $_SERVER['REMOTE_ADDR']);
		$obj->set('bStatus'      , $this->get('bStatus'));
		if( $obj->isNew() ) {
			// $this->mActionForm->set('timezone_offset', $root->mContext->getXoopsConfig('default_TZ'));
			$obj->set('bDatetime'    , date('Y-m-d H:i:s', time()) );
		} else {
			$obj->set('bDatetime'    , $this->get('bDatetime'));
		}
		$obj->set('bModified'    , date('Y-m-d H:i:s', time()) );
		$obj->set('bTitle'       , $this->get('bTitle'));
		$obj->set('bAddress'     , $this->get('bAddress'));
		$obj->set('bDescription' , $this->get('bDescription'));

		$this->mFormFile = $this->get('image');
		if ($this->mFormFile != null) {
			$obj->set('image', $this->mFormFile->getFileName());
		}

	}
}
?>